package com.util;

import com.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shkstart
 * @create 2022/3/15-14:08
 */
public class UserContext {
    public static final String EMP_IN_SESSION = "EMP_IN_SESSION";
    public static final String USER_EXPRESSIONS_IN_SESSION = "USER_EXPRESSIONS_IN_SESSION";

    /*//往session存入登录用户
    public static void setCurrentUser(Employee emp) {
        getSession().setAttribute(EMP_IN_SESSION, emp);
    }*/

    //从shiro获取当前登录用户
    public static Employee getCurrentUser() {
        return (Employee) SecurityUtils.getSubject().getPrincipal();
    }

    //获取session对象
    public static HttpSession getSession() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //往session存入登录用户的权限信息
    public static void setExpression(List<String> permissions) {
        getSession().setAttribute(USER_EXPRESSIONS_IN_SESSION,permissions);
    }
    //从session获取登录用户的权限信息
    public static List<String> getExpression() {
        return (List<String>) getSession().getAttribute(USER_EXPRESSIONS_IN_SESSION);
    }
}
